package ru.spbau.krasko.phonelock.main;

import java.util.Date;

import ru.spbau.krasko.phonelock.settings.Settings;
import ru.spbau.krasko.phonelock.utils.Constants;
import net.rim.device.api.i18n.DateFormat;
import net.rim.device.api.i18n.SimpleDateFormat;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Ui;

public class DateTimeField extends Field{
	private static final DateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm");
	private static final DateFormat DAY_OF_WEEK_FORMAT = new SimpleDateFormat("EEEE");
	private static final DateFormat DATE_FORMAT = new SimpleDateFormat("dd MMM");
	private static final int W = Constants.SCREEN_W, H = Constants.SCREEN_H / 6;

	public DateTimeField() {
		GlobalTimerExecutor.scheduleAtFixedRate(new Runnable() {			
			public void run() {
				invalidate();
			}
		}, 20 * 1000);
	}
	
	protected void layout(int width, int height) {		
        setExtent (W, H);		
	}

	protected void paint(Graphics graphics) {
		graphics.setBackgroundColor(0x0);
		graphics.setColor(0x888888);
		
		int fontH = graphics.getFont().getHeight(Ui.UNITS_px);
		
		if(!Constants.IS_FULL_VERSION) {
			String annoyingText = "Free version. Press 'help' in settings";		
			graphics.drawText(annoyingText, (W - graphics.getFont().getAdvance(annoyingText)) / 2, H / 10);
			annoyingText = "to learn about the full one.";
			graphics.drawText(annoyingText, (W - graphics.getFont().getAdvance(annoyingText)) / 2, H / 10 + fontH * 3 / 2);
		}
		
		if(Settings.isEnabled(Settings.SHOW_DATE)) {
			String date = DATE_FORMAT.formatLocal(new Date().getTime());		
			graphics.drawText(date, W / 15, H / 10 + fontH / 6);
			
			String dayOfWeek = DAY_OF_WEEK_FORMAT.formatLocal(new Date().getTime());		
			graphics.drawText(dayOfWeek, W - W / 15 - graphics.getFont().getAdvance(dayOfWeek), H / 10 + fontH / 6);
		}
		
		if(Settings.isEnabled(Settings.SHOW_TIME)) {
			graphics.setFont(graphics.getFont().derive(Font.BOLD, fontH * 3 / 2, Ui.UNITS_px));
		
			String time = TIME_FORMAT.formatLocal(new Date().getTime());		
			graphics.drawText(time, (W - graphics.getFont().getAdvance(time)) / 2, H / 10);
		}
		
	}

}
